/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_helo","_class","_spwn","_payload","_callsign","_mkrname","_type","_hint","_asloc","_stgtar","_mkrname2","_spwndir","_helosave","_grp","_pilot","_gunner","_end"];

_helo = _this select 0;
_class = _this select 1;
_spwn = _this select 2;
_payload = _this select 3;
_callsign = _this select 4;

RUM_AHel_endflight = nil;

/* checks to make sure that the helo is not already tasked */
if (_helo getVariable "RUM_inFlight") exitwith {hint "helo in flight";};

/* sets the helo as busy and removes from PDA */
_helo setVariable ["RUM_inFlight", true, true];

// _paytar = [];
RUM_ASclick = false;

/* creates the way point markers and location via map click */
openMap [true,true];
for [{_n = 1},{_n <= 3},{_n = _n + 1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_destroy";
			_hint = "Select Target Area for CAS";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Egress Point";
		};
		default {
			hint "shit";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;

	if (_n == 1) then {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = [_pos] call ROEzone",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = [_pos] call ROEzone; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	} else {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = true",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	};

	titleText [_hint, "PLAIN DOWN"];
	waitUntil {RUM_ASclick};
	
	_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	if((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	RUM_ASclick = false;
};

/* ends the map selection and starts the script */
titleFadeOut 0;
sleep 1;
openMap [false,false];

/* gets the direction of the spawn point  */
_spwndir = getdir _spwn;

/* create the helo at the spawn point */
_helosave = _helo;
_helo = createVehicle [_class, getPos _spwn, [], 0, "FLY"];
removeAllweapons _helo;
_helo setVehicleVarName _helosave;
_helo setpos [(getpos _spwn select 0),(getpos _spwn select 1),(getpos _spwn select 2)+RUM_AHEL_flyHeight];

/* set the direction of the helo */
_helo setdir _spwndir;

/* create the pilot units */
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_pilot moveinDriver _helo;
_gunner = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_gunner moveinGunner _helo;

/* set combat mode and behavior */
_helo setBehaviour "COMBAT";
_helo setCombatMode "RED";

/* dialog from pilot to player */
["immediatestrike",[player,_pilot,_callsign]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
sleep random(15);


/* create the waypoints for flight */
for [{_n = 1},{_n <= 4},{_n = _n + 1}] do {
	_mkrname = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1ingress",_helo];
			[_mkrname,_grp,_wpNum] call RUM_AHel_movewaypoint;
		};
		case 2: {
			_mkrname = format ["%1target",_helo];
			[_mkrname,_grp,_wpNum] call RUM_AHel_SAD;
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			[_mkrname,_grp,_wpNum] call RUM_AHel_movewaypoint;
		};
		case 4: {
			[_mkrname,_grp,_wpNum,_spwn] call RUM_AHel_Spawn;
		};
		default {
			hint "shit";	
		};
	};
};

/* wait for helo to get within range of the spawn point before removing the unit */
waitUntil{
	sleep 0.15;
	(!alive _helo) or (((((getPos _helo select 0) - (getpos _spwn select 0))^2+((getPos _helo select 1) - (getpos _spwn select 1))^2)^0.5) <= 300);
};

/* delete pilot, gunner, and vehicle */
deleteVehicle _pilot;
deleteVehicle _gunner;
deleteVehicle _helo;

/* delete markers */
_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarkerLocal _mkrname1;
deletemarkerLocal _mkrname2;
deletemarkerLocal _mkrname3;

/* display message after helo has been deleted */
player globalchat format["%1 has left the AO",_helo];

/* activates the unit in the PDA */
_helo setVariable ["RUM_inFlight", false, true];

/* exits script */
if(true)exitwith{};
